import { Subject } from 'rxjs';

/*
 * @Author: 吴世扬 18368095041@163.com
 * @Date: 2024-08-21 09:06:05
 * @LastEditors: 吴世扬 18368095041@163.com
 * @LastEditTime: 2024-08-21 10:02:43
 * @FilePath: /vue3-uniapp/src/service/rxjs/useForkJoin.ts
 */
export const useForkJoin = <T, D>(fn: (args: [T, D]) => void) => {
    const subject1 = new Subject<T>();
    const subject2 = new Subject<D>();

    const subscription = forkJoin([subject1, subject2]).subscribe(fn);
    onBeforeUnmount(() => {
        subscription.unsubscribe();
    });
    return [
        (data: T) => {
            subject1.next(data);
            subject1.complete();
        },
        (data: D) => {
            subject2.next(data);
            subject2.complete();
        }
    ] as [(data: T) => void, (data: D) => void];
};
